package com.gljx.activiti;

import org.activiti.engine.*;
import org.activiti.engine.task.Task;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Administrator on 2018/1/22.
 */
public class test {

    // 使用代码创建activiti需要的23个表
/*    @Test
    public void creteTable() {
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration();
        // 连接数据库的配置
        // 配置数据库驱动:对应不同数据库类型的驱动
        processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
        // 配置数据库的JDBC URL
        processEngineConfiguration
                .setJdbcUrl("jdbc:mysql://localhost:3306/wocaomysql?useUnicode=true&characterEncoding=utf8");
        // 配置连接数据库的用户名
        processEngineConfiguration.setJdbcUsername("root");
        // 配置连接数据库的密码
        processEngineConfiguration.setJdbcPassword("");
        *//**
         * public static final String DB_SCHEMA_UPDATE_FALSE =
         * "false";不能自动创建表，需要表存在 public static final String
         * DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先删除表再创建表 public static
         * final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在，自动创建表
         *//*
        processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 工作流的核心对象，ProcessEnginee对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("processEngine:" + processEngine);
    }
    // 通过xml配置文件创建流程流程引擎表
    @Test
    public void creteTableWithXml() {
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
                .createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
        System.out.println("processEngine:" + processEngine);
    }*/


    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    public static final String DemoKey = "demo1";
    private InputStream in,file;

    @Test
    public void startTask() {
        processEngine.getRepositoryService().activateProcessDefinitionByKey(DemoKey);

        processEngine.getTaskService().createTaskQuery().processDefinitionKey(DemoKey);
    }


    @Test
    public void Pic() throws IOException {
        String deploymentId ="";
        //图片资源名称
        List<String> list = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId);
        //定义图片资源名称
        String name = "";
        if(list!=null && list.size()>0) 
        {
            for(String n :list){
                name = n;
            }
        }
        InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId,name);

        //图片生成到指定路径
        File file = new File("D:/"+name);
        FileUtils.copyInputStreamToFile(in,file);

    }
    
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
}
